package com.ip.db.config;


import com.ip.common.utils.StrUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * @Description ：切换数据源Advice
 * @Author ： 解风
 * @Date ： 2017/11/24 17:16
 */
@Aspect
@Order(-1)// 保证该AOP在@Transactional之前执行
@Component
public class DynamicDataSourceAspect {

    protected Logger logger = LoggerFactory.getLogger(getClass());
    /**
     * 拦截器设置动态数据源，impl包下的service方法可以不用手动设置数据源
     * @param point JoinPoint
     * @throws Throwable
     */

    @Before("execution(* com.cq.*.service.impl.*.*(..))")
    public void select(JoinPoint point) throws Throwable {

//        System.out.println("-------------------- 88 ---------------------");
//        System.out.println("切入点方法入参列表：" + Arrays.toString(point.getArgs()));
//        System.out.println("切入点方法签名对象：" + point.getSignature());
//        System.out.println("切入点方法签名对象getName：" + point.getSignature().getName());
//        System.out.println("切入点方法签名对象getDeclaringTypeName：" + point.getSignature().getDeclaringTypeName());
//        System.out.println("切入点方法签名对象getDeclaringType：" + point.getSignature().getDeclaringType());
//        System.out.println("切入点方法签名对象getModifiers：" + point.getSignature().getModifiers());
//        System.out.println("切入点所在目标对象：" + point.getTarget());
//        System.out.println("代理对象本身：" + point.getThis());
        String methodName = point.getSignature().getName();
        String dataKey = "read";
        if(StrUtils.isNotEmpty(methodName)){
            if(methodName.endsWith("Trans")){
                dataKey = "wirte";
            } else if(methodName.startsWith("insert") || methodName.startsWith("update") || methodName.startsWith("delete")
                    || methodName.startsWith("add") || methodName.startsWith("edit") || methodName.startsWith("save")
                    || methodName.startsWith("del")){
                dataKey = "wirte";
            } else if(methodName.startsWith("query") || methodName.startsWith("select") || methodName.startsWith("get") ){
                dataKey = "read";
            } else {
                logger.info("方法定义错误!!!"+methodName);
            }
        }
//        logger.info("datasource:"+dataKey);
        DataSourceContextHolder.setDataSource(dataKey);
    }
    @Before("execution(* com.baomidou.mybatisplus.service.impl.*.*(..))")
    public void selectImpl(JoinPoint point) throws Throwable {
//        System.out.println("-------------------- 99 ---------------------");
//        System.out.println("切入点方法入参列表：" + Arrays.toString(point.getArgs()));
//        System.out.println("切入点方法签名对象：" + point.getSignature());
//        System.out.println("切入点方法签名对象getName：" + point.getSignature().getName());
//        System.out.println("切入点方法签名对象getDeclaringTypeName：" + point.getSignature().getDeclaringTypeName());
//        System.out.println("切入点方法签名对象getDeclaringType：" + point.getSignature().getDeclaringType());
//        System.out.println("切入点方法签名对象getModifiers：" + point.getSignature().getModifiers());
//        System.out.println("切入点所在目标对象：" + point.getTarget());
//        System.out.println("代理对象本身：" + point.getThis());
        String methodName = point.getSignature().getName();
        String dataKey = "read";
        if(StrUtils.isNotEmpty(methodName)){
            if(methodName.startsWith("insert") || methodName.startsWith("update") || methodName.startsWith("delete")){
                dataKey = "wirte";
            } else if( methodName.startsWith("select")){
                dataKey = "read";
            } else {
                logger.info("方法定义错误!!!");
            }
        }
//        logger.info("datasource:"+dataKey);
        DataSourceContextHolder.setDataSource(dataKey);
    }

    @After("execution(* com.cq.*.service.impl.*.*(..))")
    public void restoreDataSource(JoinPoint point) {
//        System.out.println("-------------------- 00 ---------------------");
        DataSourceContextHolder.clearDataSource();
    }

}
